﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;

namespace QuickNotepad
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
          
        }
        private List<DocumentForm> _DocumentForms = new List<DocumentForm>();

        DockContent _ActiveDocument = null;

       
        private void MainForm_Load(object sender, EventArgs e)
        {
            btnNewDocument_Click(sender, e);
        }

        private void dockPanelMain_ContentRemoved(object sender, DockContentEventArgs e)
        {
            if (_ActiveDocument != null)
            {
                if (_DocumentForms.Count > 0)
                    _DocumentForms.Remove((DocumentForm)_ActiveDocument);
            }
        }

        private void dockPanelMain_ActiveDocumentChanged(object sender, EventArgs e)
        {
            try
            {
                foreach (DockContent item in dockPanelMain.Documents)
                {
                    if (item.IsActivated)
                    {
                        _ActiveDocument = item;
                        return;
                    }

                }
            }
            catch
            {

            }
                
        }



        /// <summary>
        /// SET LANGUUAGE
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (_ActiveDocument != null)
            {
                ToolStripMenuItem active_item = sender as ToolStripMenuItem;
                foreach (ToolStripMenuItem item in languagetoolStripMenuItem.DropDownItems)
                {
                    if (active_item.Text==item.Text)
                    {
                        ((DocumentForm)_ActiveDocument).ActiveLanguage = item.Text.ToUpper();
                        item.Checked = true;
                    }
                    else
                        item.Checked = false;
                }
                
            }
            

        }

        

        private string NewDocument(string title)
        {

           
            foreach (DockContent item in dockPanelMain.Documents)
            {
                if (item.Text == title)
                {
                    if (item.Text.Contains("Untitled"))
                    {
                        title = item.Text.Substring("Untitled".Length);
                        int index = Convert.ToInt32(title.Trim());
                      
                        title = "Untitled " + (index + 1).ToString();
                    }
                }
            }
           
            return title;
        }
               

        private void btnNewDocument_Click(object sender, EventArgs e)
        {
            DocumentForm Editor = new DocumentForm(this);

            Editor.Text = NewDocument(Editor.Text);
            Editor.Name = Editor.Text.Replace(" ", "");
            if (_DocumentForms.Count <= 0)
                Editor.CloseButtonVisible = false;
            Editor.Show(dockPanelMain);
            Editor.Activate();
            _ActiveDocument = Editor;
            _DocumentForms.Add(Editor);
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            if (_ActiveDocument != null)
            {

                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "*.*|*.*";
                ofd.Multiselect = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    foreach (string item in ofd.FileNames)
                    {

                        btnNewDocument_Click(sender, e);
                        DocumentForm editor = _DocumentForms[_DocumentForms.IndexOf((DocumentForm)_ActiveDocument)];
                        string ext = Path.GetExtension(item).ToUpper();
                        editor.Text = Path.GetFileName(item).Replace(ext, "");
                        editor.Icon = Icon.ExtractAssociatedIcon(item);
                        editor.rtxtEditor.LoadFile(item);
                    }

                }

            }
        }

       

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (_ActiveDocument != null)
                {
                    DocumentForm document = ((DocumentForm)_ActiveDocument);
                    if (document.rtxtEditor.FileName != null)
                        document.rtxtEditor.SaveFile(document.rtxtEditor.FileName);
                    else
                    {
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Filter = "Text Documents (*.txt)|*.txt|All files (*.*)|*.*";
                        sfd.FileName = document.Text;
                        sfd.AddExtension = true;
                        if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Length > 0)
                        {
                            document.rtxtEditor.SaveFile(sfd.FileName);
                        }
                     
                    }
                }
            }
            catch 
            {
                
               
            }
            
        }

        private void btnSaveAll_Click(object sender, EventArgs e)
        {
            foreach (DockContent dockcontent in _DocumentForms)
            {
                DocumentForm document = ((DocumentForm)dockcontent);
                if (document.rtxtEditor.FileName != null)
                    document.rtxtEditor.SaveFile(document.rtxtEditor.FileName);
                else
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Text Documents (*.txt)|*.txt|All files (*.*)|*.*";
                    sfd.FileName = document.Text;
                    sfd.AddExtension = true;
                    if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Length > 0)
                    {
                        document.rtxtEditor.SaveFile(sfd.FileName);
                    }

                }
            }
        }

        private void btnCut_Click(object sender, EventArgs e)
        {
            if (_ActiveDocument != null)
            {
                DocumentForm document = ((DocumentForm)_ActiveDocument);
                
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {

        }

        private void btnPaste_Click(object sender, EventArgs e)
        {

        }

        private void btnUndo_ButtonClick(object sender, EventArgs e)
        {

        }

        private void btnRedo_ButtonClick(object sender, EventArgs e)
        {

        }

        private void btnOutdent_Click(object sender, EventArgs e)
        {

        }

        private void btnIndent_Click(object sender, EventArgs e)
        {

        }

        private void btnFontDialog_Click(object sender, EventArgs e)
        {

        }

        private void btnColorDialog_Click(object sender, EventArgs e)
        {

        }

        private void btnBold_Click(object sender, EventArgs e)
        {

        }

        private void btnItalic_Click(object sender, EventArgs e)
        {

        }

        private void btnUnder_Click(object sender, EventArgs e)
        {

        }

        private void btnZoom_ButtonClick(object sender, EventArgs e)
        {

        }

        private void btnFind_Click(object sender, EventArgs e)
        {

        }

       
    }
}
